package me.zhengjie.modules.weixin.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.zhengjie.config.entity.ResponseBaseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wechat/jssdk")
@Api(tags = "WEB微信配置")
public class WechatConfigController {
    private static final Logger log = LoggerFactory.getLogger(WechatConfigController.class);

    @Autowired
    private WxMpService wxMpService;

    @GetMapping(value = "/config")
    @ApiOperation("WEB-根据url获取微信配置信息")
    public ResponseBaseEntity wxJsSdkConfig(String url) {
        ResponseBaseEntity baseEntity = new ResponseBaseEntity();
        try {  // 直接调用wxMpServer 接口
            WxJsapiSignature wxJsapiSignature = wxMpService.createJsapiSignature(url);
            baseEntity.setBody(wxJsapiSignature);
            log.info("=============AccessToken="+wxMpService.getAccessToken());
            return baseEntity;
        } catch (WxErrorException e) {
            log.error("获取微信配置异常",e);
            return baseEntity;
        }
    }
}
